package com.victor.bingwallpaper.util;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.sun.jna.platform.win32.Advapi32Util;
import com.sun.jna.platform.win32.WinReg;
import com.victor.bingwallpaper.bean.Images;
import org.springframework.scheduling.annotation.Scheduled;
import org.springframework.stereotype.Component;

import javax.annotation.PostConstruct;
import java.io.IOException;
import java.time.LocalDate;
import java.time.format.DateTimeFormatter;
import java.util.Iterator;
import java.util.List;
import java.util.Map;
import java.util.Random;
import java.util.stream.Collectors;

@Component
public class WallPaper {
    private static String BING_API = "https://cn.bing.com/HPImageArchive.aspx?format=js&idx=0&n=10&nc=1612409408851&pid=hp&FORM=BEHPTB&uhd=1&uhdwidth=3840&uhdheight=2160";
    private static final Random random = new Random();
    private static String BING_URL = "https://cn.bing.com";

    public WallPaper() {
    }

    @PostConstruct
    @Scheduled(
            cron = "0 0 0 */1 * ?"
    )
    public void replaceWallPaper() throws IOException {
        String content = HttpUtils.getHttpContent(BING_API);
        JSONObject jsonObject = JSON.parseObject(content);
        JSONArray jsonArray = jsonObject.getJSONArray("images");
        jsonObject = (JSONObject)jsonArray.get(0);
        String url = BING_URL + (String)jsonObject.get("url");
        url = url.substring(0, url.indexOf("&"));
        String enddate = (String)jsonObject.get("enddate");
        LocalDate localDate = LocalDate.parse(enddate, DateTimeFormatter.BASIC_ISO_DATE);
        enddate = localDate.format(DateTimeFormatter.ISO_LOCAL_DATE);
        String copyright = (String)jsonObject.get("copyright");
        Map<String, String> imageMap = FileUtils.readReadmeMap();
        List<Images> imagesList = FileUtils.readBing();
        imagesList.set(0, new Images(copyright, enddate, url));
        imagesList = (List)imagesList.stream().distinct().collect(Collectors.toList());
        FileUtils.writeBing(imagesList);
        Iterator var10 = imagesList.iterator();

        while(var10.hasNext()) {
            Images image = (Images)var10.next();
            if (imageMap.get(image.getDate()) == null) {
                DownUtil.downPhoto(image.getDate(), image.getUrl());
            }
        }

        FileUtils.writeReadme(imagesList);
        DownUtil.getFileList();
    }

    @Scheduled(
            fixedRate = 1800000L,
            initialDelay = 3000L
    )
    public void autoChange() {
        List<String> files = DownUtil.getFileList();
        String img = (String)files.get(random.nextInt(files.size()));
        Advapi32Util.registrySetStringValue(WinReg.HKEY_CURRENT_USER, "Control Panel\\Desktop", "Wallpaper", img);
        Advapi32Util.registrySetStringValue(WinReg.HKEY_CURRENT_USER, "Control Panel\\Desktop", "WallpaperStyle", "10");
        Advapi32Util.registrySetStringValue(WinReg.HKEY_CURRENT_USER, "Control Panel\\Desktop", "TileWallpaper", "0");
        int SPI_SETDESKWALLPAPER = 20;
        int SPIF_UPDATEINIFILE = 1;
        int SPIF_SENDWININICHANGE = 2;
        Home.INSTANCE.SystemParametersInfoA(SPI_SETDESKWALLPAPER, 0, img, SPIF_UPDATEINIFILE | SPIF_SENDWININICHANGE);
    }
}
